<template>
  <el-dialog title="编辑成员" :visible.sync="showEdit" width="400px"
             @close="$emit('update:showEditMember', false)" :close-on-click-modal="false">
    <el-form label-position="right" ref="memberInfo" :rules="rules" :model="memberInfo" label-width="90px">
      <el-form-item label="成员账号:" prop="telphone">
        <el-input v-model="memberInfo.telphone" disabled></el-input>
      </el-form-item>
      <el-form-item label="成员姓名:" prop="userName">
        <el-input v-model="memberInfo.userName"></el-input>
      </el-form-item>
      <el-form-item label="邮箱地址:" prop="mailbox">
        <el-input v-model="memberInfo.mailbox"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="showEdit = false">取 消</el-button>
        <el-button type="primary" @click="editDepartment('memberInfo')">确 定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
  export default {
    name: "EditMember",
    props: {
      showEditMember: {
        type: Boolean,
        default: false
      },
      memberInfo: {
        default: () => {
          return {
            id:"",
            telphone: "",
            userName: "",
            mailbox: ""
          }
        }
      }
    },
    data() {
      return {
        showEdit: this.showEditMember,
        rules: {
          userName: [
            {required: true, message: '请输入成员姓名', trigger: 'blur'}
          ],
          mailbox: [
            {required: true, message: '请输入邮箱地址', trigger: 'blur'},
            {type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change']}
          ]
        }
      }
    },
    watch: {
      showEditMember() {
        this.showEdit = this.showEditMember
      }
    },
    methods: {
      editDepartment(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            ajax.post(apis.auth.updateMember, this.memberInfo)
              .then(res => {
                if (res.success === true) {
                  this.$message({
                    message: res.message,
                    type: 'success'
                  });
                  this.showEdit = false
                  this.$emit("refreshList")
                } else {
                  this.$message({
                    message: res.message,
                    type: 'error'
                  })
                }
              })
          }
          else {
            return false;
          }
        });
      }
    },
    created() {
    }
  }
</script>

<style lang="scss" scoped>
  .i-plus {
    text-align: center;
  }
</style>
